package com.fr.web.core.service;

import com.fr.base.CodeUtils;
import com.fr.base.FRContext;
import com.fr.base.core.BaseCoreUtils;
import com.fr.base.core.ComparatorUtils;
import com.fr.base.core.json.JSONArray;
import com.fr.base.core.json.JSONException;
import com.fr.base.core.json.JSONObject;
import com.fr.base.dav.Env;
import com.fr.base.dav.FileNode;
import com.fr.base.file.DatasourceManager;
import com.fr.data.TableData;
import com.fr.privilege.allocation.Allocation;
import com.fr.privilege.authority.AuthorityAllocation;
import com.fr.privilege.authority.Control;
import com.fr.privilege.manager.PrivilegeManager;
import com.fr.report.PaperSize;
import com.fr.report.ReportConstants;
import com.fr.util.Utils;
import com.fr.web.core.IdGenerator;
import com.fr.web.core.WebUtils;
import java.io.PrintWriter;
import java.io.Writer;
import java.util.Arrays;
import java.util.Iterator;
import java.util.Locale;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class GeneralService extends NoSessionIDService
{
  private static final String SERVERTABLEDATANAME = "utils_servertabledataname";
  private static final String SERVERTABLEDATACOLUMN = "utils_servertabledatacolumn";
  private static final String AVAILABLEFONTS = "utils_availablefonts";
  private static final String UTILS_REPORTLETS = "utils_reportlets";
  private static final String UTILS_PAPERSIZE = "utils_papersize";
  private static GeneralService GENERAL_SERVICE = new GeneralService();

  public static GeneralService getInstance()
  {
    return GENERAL_SERVICE;
  }

  public boolean accept(String paramString)
  {
    return paramString.startsWith("utils_");
  }

  public void process(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse, String paramString)
    throws Exception
  {
    if (paramString == null)
      return;
    paramString = paramString.toLowerCase();
    if ("utils_servertabledataname".equals(paramString))
      dealWithServerTableDataName(paramHttpServletRequest, paramHttpServletResponse);
    else if ("utils_servertabledatacolumn".equals(paramString))
      dealWithServerTableDataColumn(paramHttpServletRequest, paramHttpServletResponse);
    else if ("utils_availablefonts".equals(paramString))
      availableFonts(paramHttpServletRequest, paramHttpServletResponse);
    else if ("utils_reportlets".equals(paramString))
      availableReportlets(paramHttpServletRequest, paramHttpServletResponse);
    else if ("utils_papersize".equals(paramString))
      getPaperSizeByName(paramHttpServletRequest, paramHttpServletResponse);
  }

  private static void getPaperSizeByName(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse)
    throws Exception
  {
    String str = WebUtils.getHTTPRequestParameter(paramHttpServletRequest, "paperSize");
    JSONObject localJSONObject = new JSONObject();
    Locale localLocale = FRContext.getLocale();
    for (int i = 0; i < ReportConstants.PaperSizeNameSizeArray.length; ++i)
    {
      Object[] arrayOfObject = ReportConstants.PaperSizeNameSizeArray[i];
      if (ComparatorUtils.equals(str, arrayOfObject[0]))
      {
        PaperSize localPaperSize = (PaperSize)arrayOfObject[1];
        if (ComparatorUtils.equals(Locale.CHINA, localLocale))
        {
          localJSONObject.put("width", Utils.doubleToString(BaseCoreUtils.convertInchesToMillimeters(localPaperSize.getWidth())));
          localJSONObject.put("height", Utils.doubleToString(BaseCoreUtils.convertInchesToMillimeters(localPaperSize.getHeight())));
          break;
        }
        localJSONObject.put("width", Utils.doubleToString(localPaperSize.getWidth()));
        localJSONObject.put("height", Utils.doubleToString(localPaperSize.getHeight()));
        break;
      }
    }
    PrintWriter localPrintWriter = paramHttpServletResponse.getWriter();
    localPrintWriter.print(localJSONObject.toString());
    localPrintWriter.flush();
    localPrintWriter.close();
  }

  private static void dealWithServerTableDataName(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse)
    throws Exception
  {
    Object localObject2;
    JSONArray localJSONArray = new JSONArray();
    if (FRContext.getPrivilegeManager().usePrivilege())
    {
      localObject1 = FRContext.getPrivilegeManager();
      localObject2 = paramHttpServletRequest.getSession();
      String str1 = "" + ((HttpSession)localObject2).getAttribute("fr_authority");
      AuthorityAllocation localAuthorityAllocation = ((PrivilegeManager)localObject1).getControl().getAuthorityAllocation(str1);
      if (localAuthorityAllocation == null)
        break label195;
      Allocation localAllocation = localAuthorityAllocation.getAllocation();
      int i = localAllocation.getServerDSCount();
      int j = 0;
      while (true)
      {
        if (j >= i)
          break label195;
        String str2 = localAllocation.getServerDS(j);
        localJSONArray.put(new JSONObject().put("text", str2).put("value", str2));
        ++j;
      }
    }
    Object localObject1 = FRContext.getDatasourceManager().getTableDataNameIterator();
    while (((Iterator)localObject1).hasNext())
    {
      localObject2 = (String)((Iterator)localObject1).next();
      localJSONArray.put(new JSONObject().put("text", localObject2).put("value", localObject2));
    }
    label195: localObject1 = WebUtils.createPrintWriter(paramHttpServletResponse);
    localJSONArray.write((Writer)localObject1);
    ((PrintWriter)localObject1).flush();
    ((PrintWriter)localObject1).close();
  }

  private static void dealWithServerTableDataColumn(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse)
    throws Exception
  {
    String str1 = WebUtils.getHTTPRequestParameter(paramHttpServletRequest, "name");
    TableData localTableData = FRContext.getDatasourceManager().getTableData(str1);
    JSONArray localJSONArray = new JSONArray();
    int i = 0;
    int j = (localTableData != null) ? localTableData.getColumnCount() : 0;
    while (i < j)
    {
      String str2 = localTableData.getColumnName(i);
      localJSONArray.put(new JSONObject().put("text", str2).put("value", str2));
      ++i;
    }
    PrintWriter localPrintWriter = WebUtils.createPrintWriter(paramHttpServletResponse);
    localJSONArray.write(localPrintWriter);
    localPrintWriter.flush();
    localPrintWriter.close();
  }

  private static void availableFonts(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse)
    throws Exception
  {
    PrintWriter localPrintWriter = WebUtils.createPrintWriter(paramHttpServletResponse);
    localPrintWriter.print(new JSONArray(Arrays.asList(Utils.getAvailableFontFamilyNames4Report())));
    localPrintWriter.flush();
    localPrintWriter.close();
  }

  private static void availableReportlets(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse)
    throws Exception
  {
    PrintWriter localPrintWriter = WebUtils.createPrintWriter(paramHttpServletResponse);
    localPrintWriter.print(writeReporletsJSON("reportlets"));
    localPrintWriter.flush();
    localPrintWriter.close();
  }

  public static StringBuffer writeReporletsJSON(String paramString)
  {
    StringBuffer localStringBuffer1 = new StringBuffer();
    FileNode[] arrayOfFileNode = null;
    try
    {
      arrayOfFileNode = FRContext.getCurrentEnv().listFile(paramString);
    }
    catch (Exception localException)
    {
      FRContext.getLogger().log(Level.WARNING, localException.getMessage(), localException);
    }
    if ((arrayOfFileNode == null) || (arrayOfFileNode.length == 0))
      return localStringBuffer1;
    StringBuffer localStringBuffer2 = new StringBuffer();
    StringBuffer localStringBuffer3 = new StringBuffer();
    for (int i = 0; i < arrayOfFileNode.length; ++i)
    {
      FileNode localFileNode = arrayOfFileNode[i];
      String str = localFileNode.getEnvPath().substring("reportlets".length() + 1);
      if (ComparatorUtils.equals(localFileNode.getName(), ".svn"))
        break label272:
      StringBuffer localStringBuffer4 = (localFileNode.isDirectory()) ? localStringBuffer2 : localStringBuffer3;
      if (localStringBuffer4.length() > 0)
        localStringBuffer4.append(',');
      localStringBuffer4.append("{");
      localStringBuffer4.append("text:'").append(localFileNode.getName()).append('\'');
      if (!(localFileNode.isDirectory()))
      {
        localStringBuffer4.append(",");
        localStringBuffer4.append("dir:'").append(CodeUtils.cjkEncode(CodeUtils.javascriptEncode(str))).append('\'');
      }
      else if (localFileNode.isDirectory())
      {
        localStringBuffer4.append(",");
        localStringBuffer4.append("items:").append('[').append(writeReporletsJSON(localFileNode.getEnvPath())).append(']');
      }
      localStringBuffer4.append("}");
    }
    label272: localStringBuffer1.append(localStringBuffer2);
    if ((localStringBuffer2.length() > 0) && (localStringBuffer3.length() > 0))
      localStringBuffer1.append(',');
    localStringBuffer1.append(localStringBuffer3);
    return localStringBuffer1;
  }

  public static JSONArray getReportletsOfCurrentEnv(String paramString, IdGenerator paramIdGenerator)
  {
    JSONArray localJSONArray = new JSONArray();
    FileNode[] arrayOfFileNode = null;
    try
    {
      arrayOfFileNode = FRContext.getCurrentEnv().listFile(paramString);
    }
    catch (Exception localException)
    {
      FRContext.getLogger().log(Level.WARNING, localException.getMessage(), localException);
    }
    if ((arrayOfFileNode == null) || (arrayOfFileNode.length == 0))
      return localJSONArray;
    for (int i = 0; i < arrayOfFileNode.length; ++i)
    {
      FileNode localFileNode = arrayOfFileNode[i];
      if (ComparatorUtils.equals(localFileNode.getName(), ".svn"))
        break label269:
      if (!(localFileNode.isDirectory()))
      {
        int j = localFileNode.getName().lastIndexOf(".");
        if ((j >= 0) && (!(ComparatorUtils.equals(localFileNode.getName().substring(j), ".cpt"))));
      }
      else
      {
        String str = localFileNode.getEnvPath().substring("reportlets".length() + 1);
        JSONObject localJSONObject = new JSONObject();
        localJSONArray.put(localJSONObject);
        try
        {
          localJSONObject.put("id", paramIdGenerator.generateId());
          localJSONObject.put("text", localFileNode.getName());
          localJSONObject.put("reportletPath", str);
          localJSONObject.put("complete", true);
          localJSONObject.put("showcheck", true);
          localJSONObject.put("isexpand", false);
          if (localFileNode.isDirectory())
          {
            localJSONObject.put("hasChildren", true);
            localJSONObject.put("ChildNodes", getReportletsOfCurrentEnv(localFileNode.getEnvPath(), paramIdGenerator));
          }
        }
        catch (JSONException localJSONException)
        {
          localJSONException.printStackTrace();
        }
      }
    }
    label269: return localJSONArray;
  }
}